package com.imay.live.view.custom;

import android.content.Context;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.imay.base.util.ViewUtil;
import com.imay.live.R;
import com.imay.live.adapter.RoomRecommendAdapter;
import com.imay.live.model.RoomInfoModel;
import com.imay.live.view.recyclerview.GridVerticalDividerItemDecoration;

import java.util.List;

/**
 * Created by jeaman on 2017/9/15.
 */

public class RoomRecommendView extends RelativeLayout implements View.OnClickListener {
    private View mChangeView;
    private View mNoMoreView;
    private View mLikeView;
    private RecyclerView mRoomRv;
    private RoomRecommendAdapter mAdapter;
    private OnRoomRecommendClickListener mListener;

    public RoomRecommendView(Context context) {
        super(context);
        init(context);
    }

    public RoomRecommendView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public RoomRecommendView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public RoomRecommendView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context context) {
        inflate(context, R.layout.view_room_recommend, this);
        mRoomRv = (RecyclerView) findViewById(R.id.room_recommend_rv_room);
        mChangeView = findViewById(R.id.room_recommend_ll_change);
        mNoMoreView = findViewById(R.id.room_recommend_tv_no_more);
        mLikeView = findViewById(R.id.room_recommend_ll_like);

        mRoomRv.setLayoutManager(new GridLayoutManager(getContext(), 3));
        mRoomRv.setHasFixedSize(true);
        mRoomRv.addItemDecoration(new GridVerticalDividerItemDecoration.Builder(context, 0)
                .colorResId(R.color.divider_0000)
                .sizeResId(R.dimen.divider_size_4x)
                .build());

        mAdapter = new RoomRecommendAdapter(null);
        mRoomRv.setAdapter(mAdapter);

        mRoomRv.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (mListener != null) {
                    mListener.onRecommendRoomClick(mAdapter.getItem(position));
                }
            }
        });
        mChangeView.setOnClickListener(this);

        //计算item高度，保证view隐藏时高度不发生变化
        int itemHeight = (int) ((ViewUtil.getScreenWidth(context) - ViewUtil.dip2px(context, 16)) / 3);
        mRoomRv.setMinimumHeight(itemHeight);
    }

    public void setOnRoomRecommendClickListener(OnRoomRecommendClickListener listener) {
        mListener = listener;
    }

    public void setRoomData(List<RoomInfoModel> data) {
        mAdapter.setNewData(data);
        if (data == null || data.isEmpty()) {
            mLikeView.setVisibility(INVISIBLE);
            mRoomRv.setVisibility(INVISIBLE);
            mChangeView.setVisibility(INVISIBLE);
            mNoMoreView.setVisibility(VISIBLE);
        } else {
            mLikeView.setVisibility(VISIBLE);
            mRoomRv.setVisibility(VISIBLE);
            mChangeView.setVisibility(VISIBLE);
            mNoMoreView.setVisibility(INVISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.room_recommend_ll_change:
                if (mListener != null) {
                    mListener.onRecommendChangeClick();
                }
                break;
        }
    }

    public interface OnRoomRecommendClickListener {
        void onRecommendChangeClick();
        void onRecommendRoomClick(RoomInfoModel room);
    }
}
